package com.changanw.tienchin.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.changanw.tienchin.assignment.domain.Assignment;
import com.changanw.tienchin.assignment.service.IAssignmentService;
import com.changanw.tienchin.business.domain.Business;
import com.changanw.tienchin.business.domain.vo.BusinessFollow;
import com.changanw.tienchin.business.domain.vo.BusinessSummary;
import com.changanw.tienchin.business.domain.vo.BusinessSummaryEnhance;
import com.changanw.tienchin.business.domain.vo.BusinessVO;
import com.changanw.tienchin.business.mapper.BusinessMapper;
import com.changanw.tienchin.business.service.IBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changanw.tienchin.common.constant.TienChinConstants;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.domain.model.EchartsPoint;
import com.changanw.tienchin.common.core.domain.model.LineChartData;
import com.changanw.tienchin.common.utils.SecurityUtils;
import com.changanw.tienchin.follow.domain.FollowRecord;
import com.changanw.tienchin.follow.service.IFollowRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author changanw
 * @since 2023-12-06
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements IBusinessService {
  @Autowired
  BusinessMapper businessMapper;
  @Autowired
  IAssignmentService assignmentService;
  @Autowired
  IFollowRecordService recordService;

  @Override
  public List<BusinessSummary> selectBusinessList(BusinessVO businessVO) {
    return businessMapper.selectBusinessList(businessVO);
  }

  @Override
  @Transactional
  public AjaxResult addBusiness(Business business) {
    //新增之前使用手机号先查询一下
    QueryWrapper<Business> qw = new QueryWrapper<>();
    qw.lambda().eq(Business::getPhone,business.getPhone());
    Business bus = getOne(qw);
    if (bus != null){
      return AjaxResult.error("手机号重复，录入失败");
    }
    business.setStatus(TienChinConstants.BUSINESS_ALLOCATED);
    business.setCreateBy(SecurityUtils.getUsername());
    business.setCreateTime(LocalDateTime.now());
    business.setNextTime(LocalDateTime.now().plusHours(TienChinConstants.NEXT_FOLLOW_TIME));

    //添加线索
    save(business);
    //添加默认分配人
    Assignment assignment = new Assignment();
    assignment.setAssignId(business.getBusinessId());
    assignment.setLatest(true);
    assignment.setType(TienChinConstants.BUSINESS_TYPE);
    assignment.setUserId(SecurityUtils.getUserId());
    assignment.setUserName(SecurityUtils.getUsername());
    assignment.setDeptId(SecurityUtils.getDeptId());
    assignment.setCreateBy(SecurityUtils.getUsername());
    assignment.setCreateTime(LocalDateTime.now());
    assignmentService.save(assignment);
    return AjaxResult.success("商机录入成功");

  }
  @Override
  public AjaxResult getBusinessById(Integer businessId) {
    return AjaxResult.success(getById(businessId));
  }

  @Override
  @Transactional
  public AjaxResult follow(BusinessFollow businessFollow) {

    //1、线索本身的完善
    Business business = new Business();
    BeanUtils.copyProperties(businessFollow,business);
    business.setStatus(TienChinConstants.BUSINESS_FOLLOWING);
    business.setUpdateTime(LocalDateTime.now());
    updateById(business);
    //2、添加一个跟踪记录
    FollowRecord record = new FollowRecord();
    record.setAssign(business.getBusinessId());
    record.setCreateBy(SecurityUtils.getUsername());
    record.setCreateTime(LocalDateTime.now());
    record.setType(TienChinConstants.BUSINESS_TYPE);
    record.setInfo(businessFollow.getInfo());
    recordService.save(record);
    return AjaxResult.success("跟进成功");

  }

  @Override
  public AjaxResult getBusinessSummaryByBusinessId(Integer businessId) {
    Business business = getById(businessId);
    BusinessSummaryEnhance businessSummaryEnhance = new BusinessSummaryEnhance();
    BeanUtils.copyProperties(business, businessSummaryEnhance);
    return AjaxResult.success(businessSummaryEnhance);
  }

  @Override
  public AjaxResult updateClue(BusinessSummaryEnhance businessSummaryEnhance) {
    Business business = new Business();
    BeanUtils.copyProperties(businessSummaryEnhance, business);
    return updateById(business) ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");
  }

  @Override
  public AjaxResult deleteBusinessByIds(Integer[] businessIds) {
    UpdateWrapper<Business> uw = new UpdateWrapper<>();
    uw.lambda().set(Business::getDelFlag,1).in(Business::getBusinessId,businessIds);
    return update(uw) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
  }

  @Override
  public AjaxResult businessAnalysis(BusinessVO businessVO) {
    //设置一个默认时间区间
    if (businessVO.getParams().get("beginTime") == null ||
            businessVO.getParams().get("endTime") == null){
      businessVO.getParams().put("beginTime",LocalDateTime.now().minusWeeks(1));
      businessVO.getParams().put("endTime",LocalDateTime.now().plusWeeks(1));
    }
    //获取增量数据
    List<EchartsPoint> increaseData = businessMapper.increaseData(businessVO);
    //获取存量数据
    List<EchartsPoint> totalData = businessMapper.totalData(businessVO);
    LineChartData lineChartData = new LineChartData();
    lineChartData.setIncrease(increaseData);
    lineChartData.setTotal(totalData);
    return AjaxResult.success(lineChartData);
  }
}
